//
//  CNKINavViewController.m
//  CNKICommunity
//
//  Created by wei on 17/1/23.
//  Copyright © 2017年 CNKI. All rights reserved.
//

#import "CNKINavViewController.h"

@interface CNKINavViewController ()<UIGestureRecognizerDelegate>
@property(nonatomic,strong)UIView *navBarView;
@end

@implementation CNKINavViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    //侧滑手势
    self.interactivePopGestureRecognizer.delegate = self;
    UINavigationBar *bar = [UINavigationBar appearance];
    [bar setBackgroundImage:[UIImage imageNamed:@"navigationbarBackgroundWhite"] forBarMetrics:UIBarMetricsDefault];
    [bar setTitleTextAttributes:@{NSFontAttributeName : [UIFont boldSystemFontOfSize:20]}];
}
//拦截进入Nav的vc
-(void)pushViewController:(UIViewController *)viewController animated:(BOOL)animated
{
    //initWithRootViewController:vc 往里加控制器的过程 就是push的过程 所以程序已开始 往里加根视图时 就调用该方法 但初次调用时导航栏中是没有控制器的 所以下面打印数目为0，调用完之后 导航栏中才有一个 是根视图 所以下面要加判断 如果是加根视图的操作 不能干if里面的事 当第二次调用时 也就是点击了左上跳转按钮时 又会调用该方法 此时导航栏控制器数目为1 说明有根视图了 这次调用是跳转 进入if 设置被跳转页面的返回按钮
    if (self.viewControllers.count > 0)
    {
        UIButton *Backbtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [Backbtn setImage:[UIImage imageNamed:@"navigationButtonReturn"] forState:UIControlStateNormal];
        [Backbtn setImage:[UIImage imageNamed:@"navigationButtonReturnClick"] forState:UIControlStateSelected];
        [Backbtn setTitle:@"返回" forState:UIControlStateNormal];
        [Backbtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        Backbtn.size = CGSizeMake(70,30);
        Backbtn.contentEdgeInsets = UIEdgeInsetsMake(0, -10, 0, 0);
        Backbtn.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
        [Backbtn addTarget:self action:@selector(backToBeforePage) forControlEvents:UIControlEventTouchUpInside];
        
        viewController.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc]initWithCustomView:Backbtn];
        viewController.hidesBottomBarWhenPushed = YES;
    }
    [super pushViewController:viewController animated:YES];
    
}
-(void)backToBeforePage
{
    [self popViewControllerAnimated:YES];
}
-(BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer *)gestureRecognizer
{
    return self.viewControllers.count > 1;
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}



@end
